package Hander.Updata;



import View.Add.AddProjectAwardFormView;
import View.LeaderView;
import View.Updata.UpdataProjectAwardFormView;
import entity.ProjectAwardForm;
import service.PersonnelService;
import service.PersonnelServiceImpl;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class UpdataProjectAwardFormViewHandler implements ActionListener {
    private UpdataProjectAwardFormView updataProjectAwardFormView;
    private LeaderView leaderView;

    public UpdataProjectAwardFormViewHandler(UpdataProjectAwardFormView updataProjectAwardFormView, LeaderView leaderView) {
        this.updataProjectAwardFormView = updataProjectAwardFormView;
        this.leaderView = leaderView;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        JButton jButton = (JButton) e.getSource();
        String text = jButton.getText();
        if ("修改".equals(text)) {
            PersonnelService projectAwardFormService = new PersonnelServiceImpl();
            ProjectAwardForm projectAwardForm = updataProjectAwardFormView.buildProjectAwardForm();
            int key=Integer.parseInt(leaderView.getSelectedStudentIds()[0]);

            Boolean addResult = projectAwardFormService.updata(key,projectAwardForm.getAllAttributes(), updataProjectAwardFormView.getTableName());
            if (addResult) {
                leaderView.reloadTable(updataProjectAwardFormView.getTableName());
                JOptionPane.showMessageDialog(updataProjectAwardFormView, "修改成功");
                updataProjectAwardFormView.dispose();
            } else {
                System.out.println("修改错误！");
                JOptionPane.showMessageDialog(updataProjectAwardFormView, "修改失败");
            }
        }
    }
}
